import { ElMessage } from "element-plus";
import shopManagentService,{queryObject} from "../service/shopManagentService";
import { useRouter } from "vue-router";


export interface IRule{
    getList:(callback:any)=>void,
    getById:(id:any,callback:any)=>void,
    add_shop:(obj:object)=>void,
    update_Shop:(obj:object)=>void,
    queryShop:(obj:queryObject,callback:any)=>void,
    deleteById:(id:any,obj: queryObject,callback:any)=>void,
    

}
export function ShopHook(): IRule{
  const router=useRouter()
  console.log("router",router);
  
    //说取数据列表
    function getList(callback:any){
        shopManagentService.SpuAll().then((res:any)=>{
            if(res.data.code==200||res.data.code==0){
                console.log("请求信息",res.data);
                callback([...res.data.data],res.data.count)
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch(()=>{
            ElMessage("获取商品列表失败！")
        })
    }

    //条件查询
    function queryShop(obj:queryObject,callback:any){
        shopManagentService.queryShop(obj).then((res:any)=>{
            console.log("查询请求:",res.data);
            if(res.data.code==200||res.data.code==0){
                callback([...res.data.data],res.data.count)
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch(()=>{
            ElMessage("请求失败!")
        })
    }

    //根据id获取
    function getById(id:any,callback:any){
        shopManagentService.getShopById(id).then((res:any)=>{
            if(res.data.code==200||res.data.code==0){
                console.log("根据id获取:",res.data);
                callback(res.data.data)
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch(()=>{
            ElMessage("请求失败！")
        })
    }

    //添加
    function add_shop(obj:object){
        shopManagentService.addShop(obj).then((res:any)=>{
            console.log("添加请求:",res.data);
            if(res.data.code==200||res.data.code==0){
                ElMessage({type:"success",message:res.data.msg})
                router.push({name:"storeManage"})
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch((err:any)=>{
            console.log("error",err);
            ElMessage("添加请求失败！")
        })
    }

    //修改
    function update_Shop(obj:object){
        shopManagentService.updateShop(obj).then((res:any)=>{
            if(res.data.code==200||res.data.code==0){
                console.log("修改请求",res.data);
                ElMessage({ type: "success", message: res.data.msg })
                router.push({name:"storeManage"})
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch(()=>{
            ElMessage("失败")
        })
    }

    //根据id删除
    function deleteById(id:any,obj:queryObject,callback:any){
        shopManagentService.deleteShop(id).then((res:any)=>{
            console.log("删除请求:",res.data);
            if(res.data.code==200||res.data.code==0){
                ElMessage({ type: "success", message: res.data.msg })
                queryShop(obj,callback)
            }else{
                ElMessage.error(res.data.msg)
            }
        }).catch((err:any)=>{
            console.log("error",err);
            ElMessage("删除失败！")
        })
    }
    return{
        getList,
        getById,
        add_shop,
        update_Shop,
        deleteById,
        queryShop
        
    }
}